package cxt.com.recyclerview.demo4;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cxt.com.recyclerview.R;

/**
 * 作者：chengxiangtong on 2017/1/10 11:53
 * 邮箱：528440900@qq.com
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.VH> {

    private List<Image> imageList;

    public ImageAdapter(List<Image> imageList) {
        this.imageList = imageList;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_4, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(VH holder, final int position) {
        final Image image = imageList.get(position);
//        Log.i("image.getId()", image.getImgid() + "");
        holder.imageView.setImageResource(image.getImgid());
        holder.name.setText(image.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setName("haha");
                notifyItemChanged(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return imageList.size();
    }

    class VH extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView name;

        public VH(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
            name = (TextView) itemView.findViewById(R.id.name);
        }
    }
}
